package cn.good.yan.sf.t7;

import cn.good.yan.sf.t6.LinkListQueue;

/**
 * 7 单栈： 基于队列
 * (队列模拟栈)
 *
 * @author shengren.yan
 * @create 2024-07-27
 */
public class QueueToStack<E> {

    LinkListQueue<E> queue = new LinkListQueue<>(100);
    int size = 0;

    public boolean push(E e) {
        if (isFull()) {
            return false;
        }
        queue.offer(e);
        for (int i = 0; i < size; i++) {
            queue.offer(queue.pool());
        }
        size++;
        return true;
    }

    public E pol() {
        if (isEmpty()) {
            return null;
        }
        size--;
        E peek = queue.pool();
        return peek;
    }

    public E peek() {
        E peek = queue.peek();
        return peek;
    }

    public boolean isEmpty() {
        return queue.isEmpty();
    }

    public boolean isFull() {
        return size == 100;
    }


    public static void main(String[] args) {
        QueueToStack<Integer> q = new QueueToStack<>();
        System.out.println("移除元素：" + q.pol());
        System.out.println("获取头元素：" + q.peek());
        System.out.println("添加元素：" + q.push(1));
        System.out.println("添加元素：" + q.push(2));
        System.out.println("添加元素：" + q.push(3));
        System.out.println("添加元素：" + q.push(4));
        System.out.println("添加元素：" + q.push(5));
        System.out.println("添加元素：" + q.push(6));
        System.out.println("添加元素：" + q.push(7));
        System.out.println("--------------");
        System.out.println("获取头元素：" + q.peek());
        System.out.println("移除元素：" + q.pol());
        System.out.println("移除元素：" + q.pol());
        System.out.println("移除元素：" + q.pol());
        System.out.println("移除元素：" + q.pol());
        System.out.println("--------------");
    }

}
